import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'mainView',
        redirect: '/searchView',
        component: () => import('@/views/MainViews/MainView.vue'),
        children: [
            {
                path: 'searchView',
                name: 'searchView',
                component: () => import('@/views/MainViews/SearchViews/SearchView.vue'),
                children: [
                    {
                        path: 'searchResultView',
                        name: 'searchResultView',
                        redirect: '/searchResultView' //拍照后的查询结果
                    },
                    {
                        path: 'aiChat',
                        name: 'aiChat',
                        redirect: '/aiChat'
                    },
                ]
            },
            {
                path: 'medicineClockView',
                name: 'medicineClockView',
                component: () => import('@/views/MainViews/MedicineClockViews/MedicineClockView.vue'),
                children: [
                    {
                        path: 'newMedicineView',
                        name: 'newMedicineView',
                        redirect: '/editMedicineView'
                    },
                    {
                        path: 'manageMedicineView',
                        name: 'manageMedicineView',
                        redirect: '/manageMedicineView'
                    },
                ]
            },
            {
                path: 'notesView',
                name: 'notesView',
                component: () => import('@/views/MainViews/NotesViews/NotesView.vue'),
            }
        ]
    },

    /* 登录页 */
    {
        path: '/loginView',
        name: 'loginView',
        redirect: "/loginView/selectLoginType",
        component: () => import('@/views/LoginViews/LoginView.vue'),
        children: [
            {
                path: 'selectLoginType',
                name: 'selectLoginType',
                component: () => import('@/components/Login/SelectLoginType.vue'),
            },
            {
                path: 'sendVerificationCode',
                name: 'sendVerificationCode',
                component: () => import('@/components/Login/SendVerificationCode.vue'),
            },
            {
                path: 'checkVerificationCode',
                name: 'checkVerificationCode',
                component: () => import('@/components/Login/CheckVerificationCode.vue'),
            },
            {
                path: 'checkBindCode',
                name: 'checkBindCode',
                component: () => import('@/components/User/CheckBindCode.vue')
            },
        ]
    },

    {
        path: '/userView',
        name: 'userView',
        component: () => import('@/views/MainViews/SearchViews/UserViews/UserView.vue'),
        children: [
            {
                path: 'settingView',
                name: 'settingView',
                redirect: '/settingView'
            },
            {
                path: 'bindRelationshipView',
                name: 'bindRelationshipView',
                redirect: '/bindRelationshipView',
            },
            //修改个人信息页
            {
                path: 'modifyPersonalInformation',
                name: 'modifyPersonalInformation',
                redirect: '/modifyPersonalInformation'
            }
        ]
    },

    /* 用药提醒 */
    {
        path: '/editMedicineView',
        name: 'editMedicineView',
        component: () => import('@/views/MainViews/MedicineClockViews/EditMedicineView.vue'),
        children: [
            {
                path: ':pathMatch(.*)',
                component: () => import('@/views/MainViews/MedicineClockViews/EditMedicineView.vue'),
            },
        ]
    },
    {
        path: '/manageMedicineView',
        name: 'manageMedicineView',
        component: () => import('@/views/MainViews/MedicineClockViews/ManageMedicineView.vue')
    },

    /* 个人中心 */
    {
        path: '/settingView',
        name: 'settingView',
        component: () => import('../views/MainViews/SearchViews/UserViews/SettingView.vue')
    },
    {
        path: '/bindRelationshipView',
        name: 'bindRelationshipView',
        component: () => import('../views/MainViews/SearchViews/BindRelationshipView/BindRelationshipView.vue'),
    },
    {
        path: '/searchResultView',
        name: 'searchResultView',
        component: () => import('../views/MainViews/SearchViews/SearchResultView.vue')
    },
    {
        path: '/aiChat',
        name: 'aiChat',
        component: () => import('@/views/MainViews/SearchViews/AIChatView.vue')
    },

    //修改个人信息页
    {
        path: '/modifyPersonalInformation',
        name: 'modifyPersonalInformation',
        component: () => import('../views/MainViews/SearchViews/UserViews/ModifyPersonalInformation.vue')
    },

    /* 备忘录 */
    {
        path: '/editNoteView',
        name: 'editNoteView',
        component: () => import('@/views/MainViews/NotesViews/EditNoteView.vue'),
        children: [
            {
                path: ':pathMatch(.*)',
                component: () => import('@/views/MainViews/NotesViews/EditNoteView.vue'),
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes,
})

// 不需要路由跳转延时效果的地址
const noNeedRouterTimeoutPath = ['/searchView', '/medicineClockView', '/notesView']


router.beforeEach((to, from, next) => {
    // 路由跳转延时效果
    if (!noNeedRouterTimeoutPath.includes(to.path)) {
        setTimeout(() => next(), 100)
    } else next()
})


// 解决编程式导航跳转报错问题
const originalPush = router.push
const originalReplace = router.replace
router.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
router.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
    return originalReplace.call(this, location).catch(err => err)
}

export default router
